#include "nc_m1card.h" 

static U08 KeyL[6];

static U08 AuthCmd;

static U08 SNR[8];
static U08 CheckBlock;

U32 ICC_MiOne_LoadKey( U08 *keybuf )				//MifareOne卡加载密码	  	(不需要单独调用)
{
	memcpy( KeyL, keybuf, 6);
	return 0;
}
U32 ICC_MiOne_CheckKey( U08 AuthMode, U08 *Snr, U08 Block )//MifareOne卡校验密码		(不需要单独调用)
{
	AuthCmd = AuthMode;
    memcpy( SNR, Snr, 8);
    CheckBlock = Block;
	return 0;
}

//读取和写入指令都合并了校验密码													
U32 ICC_MiOne_BlkRead( U08 block,U08 cmd,U08 *key, U08 *Snr, U08 *r_buf )	//MifareOne卡块读	  		(不需要单独调用)
{
	unsigned int len=0,rcode = 0;
	int in_size = 8;
	unsigned char buf[ICC_APUD_MAXBUF*2];
	
	memset(buf, 0, ICC_APUD_MAXBUF*2);

	printf("ICC_MiOne_BlkRead:\n");
	
    buf[0] = 'R';
    sprintf(&buf[1],"%02x",block);
    sprintf(&buf[3],"%02x",cmd);
    HexToStr( key, 6, (char*)&buf[5] );
    HexToStr( Snr, in_size, (char*)&buf[17] );
	rcode = Com_Dev_TxData1( buf, 1+2*2+6*2+in_size*2, 0, 0 );
	if( rcode != 0 )
	{ DEF_SysCard_Debug; return rcode; }
	
	//接收命令
	memset(buf, 0, ICC_APUD_MAXBUF*2);
	rcode = Com_Dev_RxData1( buf, &len,  1024, 0, 0 ); 
	if( rcode != 0 )
	{ DEF_SysCard_Debug; return rcode; }
	
	if((buf[0] == 'R')&&(buf[1] == 'O'))//成功
	{
		StrToHex( (char*)&buf[2], len-2, r_buf );
		return 0;
	}
	else if((buf[0] == 'R')&&(buf[1] == 'L'))//加载密钥失败
	{
		DEF_SysCard_Debug; return 1;
	}
	else if((buf[0] == 'R')&&(buf[1] == 'C'))//校验密钥失败
	{
		DEF_SysCard_Debug; return 1;
	}
	else if((buf[0] == 'R')&&(buf[1] == 'F'))//读扇区失败
	{
		DEF_SysCard_Debug; return 1;
	}
	else 
	{   
		DEF_SysCard_Debug;return 2;
	}
}
U32 ICC_MiOne_BlkWrite( U08 block,U08 cmd,U08 *key, U08 *Snr, U08 *w_buf )		//MifareOne卡块写		 	(不需要单独调用)
{
	unsigned int len=0,rcode = 0;
	unsigned char buf[ICC_APUD_MAXBUF*2];
	
	memset(buf, 0, ICC_APUD_MAXBUF*2);

	printf("ICC_MiOne_BlkWrite:\n");
	
    buf[0] = 'W';
    sprintf(&buf[1],"%02x",block);
    sprintf(&buf[3],"%02x",cmd);
    HexToStr( key, 6, (char*)&buf[5] );
    HexToStr( Snr, 8, (char*)&buf[17] );
    HexToStr( w_buf, 16, (char*)&buf[33] );
	rcode = Com_Dev_TxData1( buf, 1+2*2+6*2+8*2+16*2, 0, 0 );
	if( rcode != 0 )
	{ DEF_SysCard_Debug; return rcode; }
	
	//接收命令
	memset(buf, 0, ICC_APUD_MAXBUF*2);
	rcode = Com_Dev_RxData1( buf, &len,  1024, 0, 0 ); 
	if( rcode != 0 )
	{ DEF_SysCard_Debug; return rcode; }
	
	if((buf[0] == 'W')&&(buf[1] == 'O'))//成功
	{
		return 0;
	}
	else if((buf[0] == 'W')&&(buf[1] == 'L'))//加载密钥失败
	{
		DEF_SysCard_Debug; return 1;
	}
	else if((buf[0] == 'W')&&(buf[1] == 'C'))//校验密钥失败
	{
		DEF_SysCard_Debug; return 1;
	}
	else if((buf[0] == 'W')&&(buf[1] == 'F'))//失败
	{
		DEF_SysCard_Debug; return 1;
	}
	else DEF_SysCard_Debug;return 2;
}

//认证校验密码和Value加值减值二合一
U32 ICC_MiOne_Value( U08 cmd, U08 block, U08 *bufw, U08 tranblock )
{
	unsigned int len=0,rcode = 0;
	int in_size = 16;
	unsigned char buf[ICC_APUD_MAXBUF*2];
	
	memset(buf, 0, ICC_APUD_MAXBUF*2);

	printf("ICC_MiOne_Value:\n");
	
    buf[0] = 'V';
 	
    sprintf(&buf[1],"%02x",CheckBlock);
    sprintf(&buf[3],"%02x",AuthCmd);
    HexToStr( KeyL, 6, (char*)&buf[5] );
    HexToStr( SNR,  8, (char*)&buf[17] ); //认证校验密码和Value加值减值二合一

    sprintf(&buf[33],"%02x",cmd);
    sprintf(&buf[35],"%02x",block);
    sprintf(&buf[37],"%02x",tranblock);
    HexToStr( bufw, in_size, (char*)&buf[39] );
	rcode = Com_Dev_TxData1( buf, 1+2*2+14*2+3*2+in_size*2, 0, 0 );
	if( rcode != 0 )
	{ DEF_SysCard_Debug; return rcode; }
	
	AuthCmd = 0;
	//接收命令
	memset(buf, 0, ICC_APUD_MAXBUF*2);
	rcode = Com_Dev_RxData1( buf, &len,  1024, 0, 0 ); 
	if( rcode != 0 )
	{ DEF_SysCard_Debug; return rcode; }
	
	if((buf[0] == 'V')&&(buf[1] == 'O'))//成功
	{
		return 0;
	}
	else if((buf[0] == 'V')&&(buf[1] == 'L'))//加载密钥失败
	{
		DEF_SysCard_Debug; return 1;
	}
	else if((buf[0] == 'V')&&(buf[1] == 'C'))//校验密钥失败
	{
		DEF_SysCard_Debug; return 1;
	}
	else if((buf[0] == 'V')&&(buf[1] == 'F'))//失败
	{
		DEF_SysCard_Debug; return 1;
	}
	else DEF_SysCard_Debug;return 2;
}
//多增加的一个函数，用来获取M1卡的物理卡号
U32 ICC_MiOne_GetPhySn( U08 *rbuf )
{
	unsigned int len=0,rcode = 0;
	int in_size = 16;
	unsigned char buf[ICC_APUD_MAXBUF*2];
	
	memset(buf, 0, ICC_APUD_MAXBUF*2);

	printf("ICC_GetPhySn:\n");
	
    buf[0] = 'G';
    sprintf(&buf[1],"%02x",1);
	rcode = Com_Dev_TxData1( buf, 3, 0, 0 );
	if( rcode != 0 )
	{ DEF_SysCard_Debug; return rcode; }
	
	//接收命令
	memset(buf, 0, ICC_APUD_MAXBUF*2);
	rcode = Com_Dev_RxData1( buf, &len,  1024, 0, 0 ); 
	if( rcode != 0 )
	{ DEF_SysCard_Debug; return rcode; }
	
	if((buf[0] == 'G')&&(buf[1] == 'O'))//成功
	{
		StrToHex( (char*)&buf[2], len-2, rbuf );
		printf("PhySn:%02x%02x%02x%02x\n",rbuf[0],rbuf[1],rbuf[2],rbuf[3]);
		return 0;
	}
	else if((buf[0] == 'G')&&(buf[1] == 'F'))//失败
	{
		DEF_SysCard_Debug; return 1;
	}
	else DEF_SysCard_Debug;return 2;
}
